#include "Input.hpp"
#include "Vector.hpp"

void Input::_CurrentToPrevious()
{
	_key_state_prev = _key_state_curr;
	_mouse_state_prev = _mouse_state_curr;
}

void Input::_PollKeyboard()
{
	al_get_keyboard_state(&_key_state_curr);
}

void Input::_PollMouse()
{
	al_get_mouse_state(&_mouse_state_curr);
}

Input::Input()
{
	ZeroMemory(&_key_state_curr, sizeof(ALLEGRO_KEYBOARD_STATE));
	ZeroMemory(&_key_state_prev, sizeof(ALLEGRO_KEYBOARD_STATE));

	ZeroMemory(&_mouse_state_curr, sizeof(ALLEGRO_MOUSE_STATE));
	ZeroMemory(&_mouse_state_prev, sizeof(ALLEGRO_MOUSE_STATE));
}

bool Input::IsKeyUp(const int key) const
{
	return !IsKeyDown(key);
}

bool Input::IsKeyDown(const int key) const
{
	return al_key_down(&_key_state_curr, key);
}

bool Input::IsKeyPressed(const int key) const
{
	return (al_key_down(&_key_state_curr, key) && !al_key_down(&_key_state_prev, key));
}

bool Input::IsKeyReleased(const int key) const
{
	return (!al_key_down(&_key_state_curr, key) && al_key_down(&_key_state_prev, key));
}

bool Input::LeftClick() const
{
	return al_mouse_button_down(&_mouse_state_curr, 1) && !al_mouse_button_down(&_mouse_state_prev, 1);
}

Vector Input::GetMousePosition() const
{
	return Vector(_mouse_state_curr.x, _mouse_state_curr.y);
}

/**
 *	This should be called once each frame to get the state of the mouse
 *	and keyboard.
 */
void Input::PollInputDevices()
{
	_CurrentToPrevious();

	_PollKeyboard();
	_PollMouse();
}